package com.study.servlet.cookie.prac01;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @Name: LoginUIServlet
* @Description: Cookie应用：记住用户名
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-3-1 下午9:19:10 
* @Version: V1.0
 */
public class LoginUIServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8") ;
		PrintWriter out = response.getWriter() ;
		String username = "" ;
		String checked = "" ;
		Cookie[] cookies = request.getCookies() ;
		if(cookies != null && cookies.length > 0) {
			for (int i = 0; i < cookies.length; i++) {
				Cookie cookie = cookies[i] ;
				if("username".equals(cookie.getName())) {
					username = cookie.getValue() ;
					checked = "checked='checked'" ;
					break ;
				}
			}
		}
		String contextPath = request.getContextPath() ;
		out.write("<form action='"+contextPath+"/servlet/LoginServlet' method='post'>");
		out.write("用户名：<input type='text' name='username' value='"+username+"' /><br/>");
		out.write("密码：<input type='password' name='password'/><br/>");
		out.write("记住用户名：<input type='checkbox' name='remember' "+checked+" /><br/>");
		out.write("<input type='submit' value='登录'/><br/>");
		out.write("</form>");
		out.flush() ;
		out.close() ;
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
